package com.niuwa.p2p.service.impl.channel;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBeanWithTitle;
import com.niuwa.p2p.core.utils.DictConstant.ChannelAuditStatus;
import com.niuwa.p2p.core.utils.DictConstant.ChlOperAuditType;
import com.niuwa.p2p.dao.channel.ChannelCustomerDao;
import com.niuwa.p2p.dao.channel.ChannelInfoDao;
import com.niuwa.p2p.dao.channel.CorpAuditLogDao;
import com.niuwa.p2p.dao.channel.CorporationDao;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.channel.ChannelInfo;
import com.niuwa.p2p.entity.channel.CorpAuditLog;
import com.niuwa.p2p.entity.channel.Corporation;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.exception.BusinessException;
import com.niuwa.p2p.service.channel.IChannelInfoService;
import com.niuwa.p2p.vo.channel.ChannelCustomerTitleVo;
import com.niuwa.p2p.vo.channel.ChannelCustomerVo;

@Service
public class ChannelInfoImpl implements IChannelInfoService {
	@Autowired
	private ChannelInfoDao channelInfoDao;
	@Autowired
	private CorpAuditLogDao corpAuditLogDao;
	@Autowired
	private ChannelCustomerDao channelCustomerDao;
	@Autowired
	private CorporationDao corporationDao;

	@Override
	public ChannelCustomerVo getChannelBasicInfo(String channelCode, User user) {
		ChannelCustomerVo channelCustomerVo = new ChannelCustomerVo();
		channelCustomerVo.setOperatorName(user.getUserName());
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("channelCode", channelCode);
		if (channelCustomerDao.selectChannelLimitByCondition(paramMap).size() ==0) {
			throw new BusinessException(
					"ChannelCustomerService.channelInfoIsNotExist");
		} else {
			channelCustomerVo = channelCustomerDao
					.selectChannelLimitByCondition(paramMap).get(0);
			channelCustomerVo.setOperatorName(user.getUserName());
		}
		return channelCustomerVo;
	}

	/**
	 * 禁用、恢復渠道狀態
	 * 
	 * @param channelCode
	 *            渠道编号
	 * 
	 * @param id
	 *            操作員ID
	 * 
	 * @param status
	 *            判断是禁用0、还是恢复1
	 * 
	 */
	@Override
	public void updateChannelStatus(String channelCode, Long id, String status) {
		int result = 0;
		// 取得渠道信息
		ChannelInfo channelInfo = channelInfoDao
				.selectByPrimaryKey(channelCode);
		// 不存在,抛出错误
		if (channelInfo == null) {
			throw new BusinessException(
					"ChannelCustomerService.channelInfoIsNotExist");
		}
		// 添加渠道审核操作记录
		CorpAuditLog corpAuditLog = new CorpAuditLog();
		corpAuditLog.setCorpCode(channelCode);
		corpAuditLog.setCreateTime(new Date());
		corpAuditLog.setOperator(id);
		// 取得企业信息
		Corporation corporation = corporationDao.selectByPrimaryKey(channelCode);
		// 不存在,抛出错误
		if (corporation == null) {
			throw new BusinessException("EnterpriseinfoMgService.corporationIsNotExist");
		}	
		// 设置渠道状态
		if ("0".equals(status)) {
			corporation.setAuditStatus(ChannelAuditStatus.FORBIDDEN.getCode());
			corpAuditLog.setOperateType("FORBIDDEN");
		} else {
			corporation.setAuditStatus(ChannelAuditStatus.PASS.getCode());
			corpAuditLog.setOperateType("PASS");
		}
		// 更新企业信息
		result = corporationDao.updateByPrimaryKeySelective(corporation);
		if(result != 1) {
			throw new BusinessException("EnterpriseinfoMgService.failTOEnableCorporation");
		}
		result = corpAuditLogDao.insert(corpAuditLog);
		if (result != 1) {
			throw new BusinessException(
					"ChannelInforService.failTOAddChannelAuditLog");
		}
	}

	/**
	 * 分页带title查询渠道列表
	 * 
	 * @param paramMap
	 * @return
	 */
	@Override
	public PageBeanWithTitle<ChannelCustomerTitleVo, ChannelCustomerVo> searchIncludeTitle(
			Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;

		int totalCount = channelCustomerDao.countNoCondition();

		PageBeanWithTitle<ChannelCustomerTitleVo, ChannelCustomerVo> page = new PageBeanWithTitle<ChannelCustomerTitleVo, ChannelCustomerVo>(
				totalCount);

		// 设置当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		page.setRows(channelCustomerDao.selectChannelLimitByCondition(paramMap));

		page.setTitleItem(getChannelCustomeTitleSummerItem());

		return page;
	}

	/**
	 * 取得渠道title的合计金额
	 * 
	 * @return
	 */
	private ChannelCustomerTitleVo getChannelCustomeTitleSummerItem() {
		ChannelCustomerTitleVo channelCustomerTitleVo = new ChannelCustomerTitleVo();
		channelCustomerTitleVo.setAllApproveAmount(channelCustomerDao
				.selectAllApproveAmount() == null ? BigDecimal.ZERO
				: channelCustomerDao.selectAllApproveAmount());
		channelCustomerTitleVo.setAllUseableAmount(channelCustomerDao
				.selectAllUseableAmount() == null ? BigDecimal.ZERO
				: channelCustomerDao.selectAllUseableAmount());
		channelCustomerTitleVo.setAllUsedAmount(channelCustomerDao
				.selectAllUsedAmount() == null ? BigDecimal.ZERO
				: channelCustomerDao.selectAllUsedAmount());
		return channelCustomerTitleVo;
	}

	/**
	 * 更新渠道批准額度
	 * 
	 * @param channelCode
	 * @param id
	 * @param status
	 */
	@Override
	public void updateChannelApproveAmount(String channelCode, Long id, String remark, BigDecimal changeAmount) {
		ChannelInfo channelinfo=channelInfoDao.selectByPrimaryKey(channelCode);
		int result = 1;
		// 更新渠道信息
		ChannelInfo channelInfo=new ChannelInfo();
		channelInfo.setChannelCode(channelCode);
		if(channelinfo.getApproveAmount()==null){
			channelInfo.setApproveAmount(changeAmount);
		}else{
			channelInfo.setApproveAmount(changeAmount.add(channelinfo.getApproveAmount()));
		}
		if(channelinfo.getUsableAmount()==null){
			channelInfo.setUsableAmount(changeAmount);
		}else{
			channelInfo.setUsableAmount(changeAmount.add(channelinfo.getUsableAmount()));
		}
		result = channelInfoDao.updateApproveAmountByPrimaryKey(channelInfo);
		if (result != 1) {
			throw new BusinessException(
					"ChannelInfoService.failTOUpdateChannelApproveAmount");
		}
		// 添加渠道审核操作记录
		CorpAuditLog corpAuditLog = new CorpAuditLog();
		corpAuditLog.setCorpCode(channelCode);
		corpAuditLog.setCreateTime(new Date());
		corpAuditLog.setOperator(id);
		// 设置渠道状态
		corpAuditLog.setOperateType(ChlOperAuditType.PASS.getCode());
		result = corpAuditLogDao.insert(corpAuditLog);
		if (result != 1) {
			throw new BusinessException(
					"ChannelInforService.failTOAddChannelAuditLog");
		}

	}
	
	@Override
	public List<Corporation> loadChannelInfo() {
		return channelCustomerDao.loadChannelInfo();
	}
}
